package com.mrwang.controller;

import com.mrwang.pojo.Category;
import com.mrwang.service.CategoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Resource
    CategoryService categoryService;

    @GetMapping("list")
    public ResponseEntity<List<Category>> categoryList(@RequestParam("pid") Long pid){
        if (pid==null || pid.longValue()<0){
            return ResponseEntity.badRequest().build();
        }
        List<Category> categoryList= categoryService.categoryList(pid);
        if (CollectionUtils.isEmpty(categoryList)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(categoryList);
    }

    @GetMapping("names")
    public ResponseEntity<List<String>> queryNamesByIds(@RequestParam("ids") List<Long> ids){
        List<String> strings= categoryService.queryNamesByIds(ids);
        if (CollectionUtils.isEmpty(strings)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(strings);
    }

}
